package com.hz.monitoring.module.hardware.controller.admin.sensor_sys_type;

import com.hz.monitoring.framework.common.pojo.CommonResult;
import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.excel.core.util.ExcelUtils;
import com.hz.monitoring.framework.operatelog.core.annotations.OperateLog;
import com.hz.monitoring.module.hardware.controller.admin.sensor_sys_type.vo.*;
import com.hz.monitoring.module.hardware.convert.sensor_sys_type.SensorSysTypeConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.sensor_sys_type.SensorSysTypeDO;
import com.hz.monitoring.module.hardware.service.sensor_sys_type.SensorSysTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.hz.monitoring.framework.common.pojo.CommonResult.success;
import static com.hz.monitoring.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 传感器系统分类")
@RestController
@RequestMapping("/hardware/sensor-sys-type")
@Validated
public class SensorSysTypeController {

    @Resource
    private SensorSysTypeService sensorSysTypeService;

    @PostMapping("/create")
    @ApiOperation("创建传感器系统分类")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-sys-type:create')")
    public CommonResult<Long> createSensorSysType(@Valid @RequestBody SensorSysTypeCreateReqVO createReqVO) {
        return success(sensorSysTypeService.createSensorSysType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新传感器系统分类")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-sys-type:update')")
    public CommonResult<Boolean> updateSensorSysType(@Valid @RequestBody SensorSysTypeUpdateReqVO updateReqVO) {
        sensorSysTypeService.updateSensorSysType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除传感器系统分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:sensor-sys-type:delete')")
    public CommonResult<Boolean> deleteSensorSysType(@RequestParam("id") Long id) {
        sensorSysTypeService.deleteSensorSysType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得传感器系统分类")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:sensor-sys-type:query')")
    public CommonResult<SensorSysTypeRespVO> getSensorSysType(@RequestParam("id") Long id) {
        SensorSysTypeDO sensorSysType = sensorSysTypeService.getSensorSysType(id);
        return success(SensorSysTypeConvert.convert(sensorSysType));
    }

    @GetMapping("/list")
    @ApiOperation("获得传感器系统分类列表")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-sys-type:query')")
    public CommonResult<List<SensorSysTypeRespVO>> getSensorSysTypeList(SensorSysTypeListReqVO cmd) {
        List<SensorSysTypeDO> list = sensorSysTypeService.getSensorSysTypeList(cmd);
        return success(SensorSysTypeConvert.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得传感器系统分类分页")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-sys-type:query')")
    public CommonResult<PageResult<SensorSysTypeRespVO>> getSensorSysTypePage(@Valid SensorSysTypePageReqVO pageVO) {
        PageResult<SensorSysTypeDO> pageResult = sensorSysTypeService.getSensorSysTypePage(pageVO);
        return success(SensorSysTypeConvert.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出传感器系统分类 Excel")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-sys-type:export')")
    @OperateLog(type = EXPORT)
    public void exportSensorSysTypeExcel(@Valid SensorSysTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SensorSysTypeDO> list = sensorSysTypeService.getSensorSysTypeList(exportReqVO);
        // 导出 Excel
        List<SensorSysTypeExcelVO> datas = SensorSysTypeConvert.convertList02(list);
        ExcelUtils.write(response, "传感器系统分类.xls", "数据", SensorSysTypeExcelVO.class, datas);
    }

}
